package com.wbpay.sdk.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wbpay.sdk.framework.IUserFunction;
import com.wbpay.sdk.params.User;
import com.wbpay.sdk.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 账号弹出框
 */
public class AccountPopup extends PopupWindow {
    private final Context mContext;
    private PopAdapter mPopAdapter;
    private IUserFunction mUserFunction;

    public AccountPopup(Context context, IUserFunction f) {
        mContext = context;
        mUserFunction = f;
        init();
    }

    private void init() {
        ListView listview = new ListView(mContext);
        mPopAdapter = new PopAdapter(mContext);
        listview.setAdapter(mPopAdapter);
        listview.setFocusable(true);

        setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(listview);
    }

    public void showPopup(EditText editText, List<User> userList) {
        if (userList != null && userList.size() > 0) {
            mPopAdapter.addAll(userList);
            int width = editText.getWidth();
            View anchorView = editText;
            int padding = 0;
            try {
                ViewGroup viewGroup = (ViewGroup) editText.getParent();
                anchorView = viewGroup;
                padding = viewGroup.getPaddingLeft();
                width = viewGroup.getWidth();
            } catch (Exception e) {
            }
            setWidth(width - padding / 2);
            setHeight(-2);
            showAsDropDown(anchorView, padding / 4, -padding / 4);
        }
    }

    class PopAdapter extends ArrayAdapter<User> implements View.OnClickListener {
        private AbsListView.LayoutParams mParams = new AbsListView.LayoutParams(-1, Utils.dp2px(mContext, 48));

        public PopAdapter(Context context) {
            super(context, 0, new ArrayList<User>());
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                int layoutId = Utils.getLayoutId(mContext, "jy_item_accounts");
                convertView = LayoutInflater.from(mContext).inflate(layoutId, null);
                holder.textView = (TextView) convertView.findViewById(Utils.getId(mContext, "text_account"));
                holder.textView.setOnClickListener(this);
                holder.imageView = (ImageView) convertView.findViewById(Utils.getId(mContext, "button_delete"));
                holder.imageView.setOnClickListener(this);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            convertView.setLayoutParams(mParams);
            holder.textView.setTag(position);
            holder.imageView.setTag(position);
            User user = getItem(position);
            if (user != null) {
                holder.textView.setText(user.getUsername());
            }
            return convertView;
        }

        @Override
        public void onClick(View v) {
            dismiss();
            if (v.getId() == Utils.getId(mContext, "button_delete")) {
                int position = -1;
                try {
                    position = (int) v.getTag();
                } catch (Exception e) {
                }
                if (position > -1) {
                    removeUser(getItem(position));
                }
            } else if (v.getId() == Utils.getId(mContext, "text_account")) {
                int position = -1;
                try {
                    position = (int) v.getTag();
                } catch (Exception e) {
                }
                if (position > -1) {
                    fillUser(getItem(position));
                }
            }
        }
    }

    private void removeUser(User user) {
        if (mUserFunction != null) {
            mUserFunction.removeUser(user);
        }
    }

    private void fillUser(User user) {
        if (mUserFunction != null) {
            mUserFunction.fillUser(user);
        }
    }

    class ViewHolder {
        TextView textView;
        ImageView imageView;
    }
}
